import random
from data import *

def ResetTable():
    number_table.clear()
    for i in range(XLine * YLine):
        number_table.append(0)
    x = random.randint(0, XLine * YLine - 1)
    y = random.randint(0, XLine * YLine - 1)
    while y == x:
        y = random.randint(0, XLine * YLine - 1)
    number_table[x] = random.randint(1, 2) * 2
    number_table[y] = random.randint(1, 2) * 2

def CheckLose():
    for i in range(XLine):
        for j in range(YLine):
            if number_table[i * XLine + j] == 0:
                return False
            elif i != 0 and number_table[i * XLine + j] == number_table[(i - 1) * XLine + j]:
                return False
            elif j != 0 and number_table[i * XLine + j] == number_table[i * XLine + j - 1]:
                return False
            elif i != XLine - 1 and number_table[i * XLine + j] == number_table[(i + 1) * XLine + j]:
                return False
            elif j != YLine - 1 and number_table[i * XLine + j] == number_table[i * XLine + j + 1]:
                return False
    return True

def CheckWin():
    for i in range(XLine * YLine):
        if number_table[i] >= GoalToWin:
            return True
    return False

def PrintTable():
    print('=' * 80 + '\n')
    for i in range(XLine):
        for j in range(YLine):
            if number_table[i * XLine + j]:
                print('%4d' % number_table[i * XLine + j], end='\t\t')
            else:
                print('   -', end='\t\t')
        print('\n')

def JudgeOfMoveUp():
    for j in range(YLine):
        for i in range(XLine - 1, 0, -1):
            if number_table[i * XLine + j] != 0 and\
                    (number_table[(i - 1) * XLine + j] == 0 or number_table[i * XLine + j] == number_table[(i - 1) * XLine + j]):
                return True
    return False

def JudgeOfMoveDown():
    for j in range(YLine):
        for i in range(XLine - 1):
            if number_table[i * XLine + j] != 0 and\
                    (number_table[(i + 1) * XLine + j] == 0 or number_table[i * XLine + j] == number_table[(i + 1) * XLine + j]):
                return True
    return False

def JudgeOfMoveLeft():
    for i in range(XLine):
        for j in range(YLine - 1, 0, -1):
            if number_table[i * XLine + j] != 0 and\
                    (number_table[i * XLine + j - 1] == 0 or number_table[i * XLine + j] == number_table[i * XLine + j - 1]):
                return True
    return False

def JudgeOfMoveRight():
    for i in range(XLine):
        for j in range(YLine - 1):
            if number_table[i * XLine + j] != 0 and\
                    (number_table[i * XLine + j + 1] == 0 or number_table[i * XLine + j] == number_table[i * XLine + j + 1]):
                return True
    return False

def MoveUp():
    for j in range(YLine):
        for i in range(XLine - 1):
            for ii in range(i + 1, XLine):
                if number_table[ii * XLine + j] != 0:
                    if number_table[i * XLine + j] == 0:
                        number_table[i * XLine + j] = number_table[ii * XLine + j]
                        number_table[ii * XLine + j] = 0
                    else:
                        if number_table[i * XLine + j] == number_table[ii * XLine + j]:
                            number_table[i * XLine + j] *= 2
                            number_table[ii * XLine + j] = 0
                        break

def MoveDown():
    for j in range(YLine):
        for i in range(XLine - 1, 0, -1):
            for ii in range(i - 1, -1, -1):
                if number_table[ii * XLine + j] != 0:
                    if number_table[i * XLine + j] == 0:
                        number_table[i * XLine + j] = number_table[ii * XLine + j]
                        number_table[ii * XLine + j] = 0
                    else:
                        if number_table[i * XLine + j] == number_table[ii * XLine + j] :
                            number_table[i * XLine + j] *= 2
                            number_table[ii * XLine + j] = 0
                        break

def MoveRight():
    for i in range(XLine):
        for j in range(YLine - 1, 0, -1):
            for jj in range(j - 1, -1, -1):
                if number_table[i * XLine + jj] != 0:
                    if number_table[i * XLine + j] == 0:
                        number_table[i * XLine + j] = number_table[i * XLine + jj]
                        number_table[i * XLine + jj] = 0
                    else:
                        if number_table[i * XLine + j] == number_table[i * XLine + jj]:
                            number_table[i * XLine + j] *= 2
                            number_table[i * XLine + jj] = 0
                        break

def MoveLeft():
    for i in range(XLine):
        for j in range(YLine - 1):
            for jj in range(j + 1, YLine):
                if number_table[i * XLine + jj] != 0:
                    if number_table[i * XLine + j] == 0:
                        number_table[i * XLine + j] = number_table[i * XLine + jj]
                        number_table[i * XLine + jj] = 0
                    else:
                        if number_table[i * XLine + j] == number_table[i * XLine + jj]:
                            number_table[i * XLine + j] *= 2
                            number_table[i * XLine + jj] = 0
                        break

def InputOfCommand():
    if CheckLose():
        char = input('遗憾，你输了！输入R再来一盘，或"exit"退出：')
        while char != 'r' and char != 'R' and char.lower() != 'exit':
            char = input('输入有误，输了就别矫情了，重新输入吧：')
    elif CheckWin():
        char = input('恭喜，你赢了！输入R再来一盘，或"exit"退出：')
        while char != 'r' and char != 'R' and char.lower() != 'exit':
            char = input('输入有误，赢了也不要骄傲，重新输入吧：')
    else:
        char = input('输入W/A/S/D（上/下/左/右）来移动，或R重新开始，以及"exit"退出：')
        while True:
            if (len(char) != 1 or char not in 'wasdrWASDR') and char.lower() != 'exit':
                char = input('输入有误，请不要操之过急，重新输入吧：')
            elif not JudgeOfMoveUp() and char in 'wW':
                char = input('不能向上移动！重新输入吧：')
            elif not JudgeOfMoveDown() and char in 'sS':
                char = input('不能向下移动！重新输入吧：')
            elif not JudgeOfMoveLeft() and char in 'aA':
                char = input('不能向左移动！重新输入吧：')
            elif not JudgeOfMoveRight() and char in 'dD':
                char = input('不能向右移动！重新输入吧：')
            else:
                break
    return char

def AddNewNumber():
    num_of_blank = 0
    for i in range(XLine * YLine):
        if number_table[i] == 0:
            num_of_blank += 1
    x = random.randint(1, num_of_blank)
    for i in range(XLine * YLine):
        if number_table[i] == 0:
            x -= 1
        if not x:
            break
    number_table[i] = random.randint(1, 2) * 2




if __name__ == '__main__':
    pass
